home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Mindy / Mindy 1.2 - portable sources / comp / lexer.l < prev    next >
Encoding:
Lex Description  |  1995-03-15  |  6.2 KB  |  270 lines  |  [TEXT/ttxt]

  1. %{
  2. /**********************************************************************\
  3. *
  4. *  Copyright (c) 1994  Carnegie Mellon University
  5. *  All rights reserved.
  6. *  
  7. *  Use and copying of this software and preparation of derivative
  8. *  works based on this software are permitted, including commercial
  9. *  use, provided that the following conditions are observed:
  10. *  
  11. *  1. This copyright notice must be retained in full on any copies
  12. *     and on appropriate parts of any derivative works.
  13. *  2. Documentation (paper or online) accompanying any system that
  14. *     incorporates this software, or any part of it, must acknowledge
  15. *     the contribution of the Gwydion Project at Carnegie Mellon
  16. *     University.
  17. *  
  18. *  This software is made available "as is".  Neither the authors nor
  19. *  Carnegie Mellon University make any warranty about the software,
  20. *  its performance, or its conformity to any specification.
  21. *  
  22. *  Bug reports, questions, comments, and suggestions should be sent by
  23. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  24. *
  25. ***********************************************************************
  26. *
  27. * $Header: lexer.l,v 1.12 94/10/05 20:55:19 nkramer Exp $
  28. *
  29. * This file is the lexical analizer.
  30. *
  31. \**********************************************************************/
  32.  
  33. #include <stdio.h>
  34. #include "lexer.h"
  35. #include "src.h"
  36. #include "parser.tab.h"
  37.  
  38. extern int isatty();
  39. extern void warn();
  40.  
  41. int line_count = 1;
  42.  
  43. #define is(type) return (yylval.token = make_token(yytext, yyleng)), type;
  44.  
  45. static void skip_multi_line_comment(void);
  46. static int make_header_key(void);
  47. static int make_header_val(void);
  48. static int make_header_end(void);
  49.   
  50. %}
  51.   
  52. %x ini key val etc
  53.  
  54. D    [0-9]
  55. E    [esdx][-+]?{D}+
  56.  
  57. A    [a-z]
  58. G    [!&*<=>|^$%@_]
  59. S    [-+~?/]
  60.  
  61. DGS    ({D}|{G}|{S})
  62. ADGS    ({A}|{DGS})
  63.  
  64. N    ((({G}{DGS}*)?{A}{ADGS}*)|({D}{DGS}*({A}{DGS}+)*{A}{A}{ADGS}*))
  65.  
  66. O    ":="|"+"|"-"|"*"|"/"|"="|"=="|"<"|">"|"<="|">="|"~="|"&"|"|"|"^"
  67.  
  68. STR    \"(([ !#-\[\]-~])|(\\["\\abefnrt0]))*\"
  69.  
  70. %%
  71.  
  72. <INITIAL>.            BEGIN(ini); yyless(0);
  73.  
  74. <ini>^#!.*\n            BEGIN(key); warn(line_count, "igoring initial #! interpreter comment\n"); line_count++; 
  75.  
  76. <ini,key>^[a-z][-a-z0-9]*:    BEGIN(val); return make_header_key();
  77. <ini,key>^[ \t]*\n        BEGIN(etc); return make_header_end();
  78.  
  79. <val>.*\n([ \t]+.+\n)*        BEGIN(key); return make_header_val();
  80.  
  81. <etc>[ \t\f]+            ;
  82. <etc>[\n]            line_count++;
  83.  
  84. <etc>"//".*            ;
  85. <etc>"/*"            skip_multi_line_comment();
  86.  
  87. <etc>abstract            is(ABSTRACT);
  88. <etc>above            is(ABOVE);
  89. <etc>begin            is(DBEGIN);
  90. <etc>below            is(BELOW);
  91. <etc>block            is(BLOCK);
  92. <etc>by                is(BY);
  93. <etc>case            is(CASE);
  94. <etc>class            is(CLASS);
  95. <etc>cleanup            is(CLEANUP);
  96. <etc>concrete            is(CONCRETE);
  97. <etc>constant            is(CONSTANT);
  98. <etc>define            is(DEFINE);
  99. <etc>else            is(ELSE);
  100. <etc>elseif            is(ELSEIF);
  101. <etc>end            is(END);
  102. <etc>exception            is(EXCEPTION);
  103. <etc>finally            is(FINALLY);
  104. <etc>for            is(FOR);
  105. <etc>free            is(FREE);
  106. <etc>from            is(FROM);
  107. <etc>generic            is(GENERIC);
  108. <etc>handler            is(HANDLER);
  109. <etc>if                is(IF);
  110. <etc>in                is(IN);
  111. <etc>inherited            is(INHERITED);
  112. <etc>instance            is(INSTANCE);
  113. <etc>keyed-by            is(KEYED_BY);
  114. <etc>keyword            is(KEYWORD_RESERVED_WORD);
  115. <etc>let            is(LET);
  116. <etc>local            is(LOCAL);
  117. <etc>method            is(METHOD);
  118. <etc>open            is(OPEN);
  119. <etc>otherwise            is(OTHERWISE);
  120. <etc>primary            is(PRIMARY);
  121. <etc>required            is(REQUIRED);
  122. <etc>seal            is(SEAL);
  123. <etc>sealed            is(SEALED);
  124. <etc>select            is(SELECT);
  125. <etc>slot            is(SLOT);
  126. <etc>subclass            is(SUBCLASS);
  127. <etc>then            is(THEN);
  128. <etc>to                is(TO);
  129. <etc>unless            is(UNLESS);
  130. <etc>until            is(UNTIL);
  131. <etc>variable            is(VARIABLE);
  132. <etc>virtual            is(VIRTUAL);
  133. <etc>while            is(WHILE);
  134.  
  135. <etc>module            is(MODULE);
  136. <etc>library            is(LIBRARY);
  137. <etc>export            is(EXPORT);
  138. <etc>create            is(CREATE);
  139. <etc>use            is(USE);
  140. <etc>all            is(ALL);
  141.  
  142. <etc>"prefix:"            is(PREFIX_OPTION);
  143. <etc>"import:"            is(IMPORT_OPTION);
  144. <etc>"exclude:"            is(EXCLUDE_OPTION);
  145. <etc>"export:"            is(EXPORT_OPTION);
  146. <etc>"rename:"            is(RENAME_OPTION);
  147.  
  148. <etc>"("            is(LPAREN);
  149. <etc>")"            is(RPAREN);
  150. <etc>","            is(COMMA);
  151. <etc>"."            is(DOT);
  152. <etc>";"            is(SEMI);
  153. <etc>"["            is(LBRACKET);
  154. <etc>"]"            is(RBRACKET);
  155. <etc>"{"            is(LBRACE);
  156. <etc>"}"            is(RBRACE);
  157. <etc>"::"            is(COLON_COLON);
  158. <etc>"-"            is(MINUS);
  159. <etc>"~"            is(TILDE);
  160. <etc>"="            is(EQUAL);
  161. <etc>"=="            is(EQUAL_EQUAL);
  162. <etc>"=>"            is(ARROW);
  163. <etc>"#("            is(SHARP_PAREN);
  164. <etc>"#["            is(SHARP_BRACKET);
  165. <etc>"#t"            is(SHARP_T);
  166. <etc>"#f"            is(SHARP_F);
  167. <etc>"#next"            is(NEXT);
  168. <etc>"#rest"            is(REST);
  169. <etc>"#key"            is(KEY);
  170. <etc>"#all-keys"        is(ALL_KEYS);
  171.  
  172. <etc>[-+]?{D}+            is(INTEGER);
  173. <etc>#x[0-9a-f]+        is(INTEGER);
  174. <etc>#o[0-7]+            is(INTEGER);
  175. <etc>#b[01]+            is(INTEGER);
  176.  
  177. <etc>[-+]?{D}*\.{D}+{E}?    is(FLOAT);
  178. <etc>[-+]?{D}+\.{D}*{E}?    is(FLOAT);
  179. <etc>[-+]?{D}+{E}        is(FLOAT);
  180.  
  181. <etc>'[ -&(-\[\]-~]'        is(CHARACTER);
  182. <etc>'\\['\\abefnrt0]'        is(CHARACTER);
  183.  
  184. <etc>{STR}            is(STRING);
  185.  
  186. <etc>{O}            is(BINARY_OPERATOR);
  187. <etc>{N}            is(SYMBOL);
  188. <etc>\\{O}            is(SYMBOL);
  189. <etc>{N}:            is(KEYWORD);
  190. <etc>#{STR}            is(SYMBOL_LITERAL);
  191.  
  192. <ini,key,val,etc>.        is(BOGUS);
  193.  
  194. %%
  195.  
  196. static void skip_multi_line_comment(void)
  197. {
  198.     int depth = 1;
  199.     int c, prev = '\0';
  200.     
  201.     while (1) {
  202.     c = input();
  203.     switch (c) {
  204.       case EOF:
  205.         return;
  206.       case '\n':
  207.         line_count++;
  208.         prev = c;
  209.         break;
  210.       case '/':
  211.         if (prev == '*')
  212.         if (--depth == 0)
  213.             return;
  214.         else
  215.             prev = 0;
  216.         else
  217.         prev = c;
  218.         break;
  219.       case '*':
  220.         if (prev == '/') {
  221.         depth++;
  222.         prev = 0;
  223.         }
  224.         else
  225.         prev = c;
  226.         break;
  227.       default:
  228.         prev = c;
  229.         break;
  230.     }
  231.     }
  232. }
  233.  
  234. static int make_header_key()
  235. {
  236.   yylval.token = make_token(yytext, yyleng-1);
  237.   return HEADER_KEY;
  238. }
  239. static int make_header_val()
  240. {
  241.   char *p1, *p2;
  242.   int skipped = 0;
  243.  
  244.   for (p1 = p2 = yytext; p1 < yytext + yyleng; ) {
  245.     /* skip initial spaces */
  246.     while (p1 < yytext + yyleng && (*p1 == ' ' || *p1 == '\t')) {
  247.       p1 += 1;
  248.       skipped += 1;
  249.     }
  250.     /* copy to end of line */
  251.     while (p1 < yytext + yyleng && *p1 != '\n') {
  252.       *p2++ = *p1++;
  253.     }
  254.     /* copy and count newline */
  255.     *p2++ = *p1++;
  256.     line_count += 1;
  257.   }
  258.   /* make the token, dropping the last newline */
  259.   yylval.token = make_token(yytext, yyleng - skipped - 1);
  260.   return HEADER_VAL;
  261. }
  262. static int make_header_end()
  263. {
  264.   line_count += 1;
  265.   yylval.token = yylval.token = make_token(yytext, yyleng);
  266.   return HEADER_END;
  267. }
  268.  
  269. int yywrap() { return 1; }
  270.